/**
 * Created by no1 on 2015/9/5.
 */
'use strict';

let DaoBase = require('./DaoBase');
let Model = require('./models/orderTicket.js');
let db = require('./models');
let D = require('./function');
let F = require('../../function');

var util = require("util");

function OrderTicketDao(data) {
  data = data || {};
  data.model = data.model || db.orderTicket;
  DaoBase.call(this, data);
}

util.inherits(OrderTicketDao, DaoBase);

/**
 *@todo 查询
 *@param  {Object}  query  查询的内容
 *@param  {Object}  opt    其他参数
 */
OrderTicketDao.prototype.getByQuery = function*(query, page,pageSize) {
  page = page || 1;
  pageSize = pageSize || 10;
  let offset = (page - 1) * pageSize;
  let ret = yield  this.model.findAll({where: query,limit: pageSize,offset: offset});
  if (!ret) return F.returnData(1, '查询失败');
  return F.returnData(0, '查询成功.', ret)
};

/**
 *@todo 查询并保存
 *@param  {Object}  query  查询的内容
 *@param  {Object}  doc    保存的数据
 */
OrderTicketDao.prototype.findAndSave = function*(query, doc) {
  let ret = yield this.findOneForSave(query);
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  if (ret.data) return F.returnData(1, JSON.stringify(query) + '时,已经有一条相同的记录');
  let result = yield this.save(doc);
  return F.returnData(0, result.msg, result.data);
};

/**
 *@todo 查询并保存
 *@param where  查询的内容
 *@param update 保存的数据
 */
OrderTicketDao.prototype.update = function*(where, update) {
  return yield Model.update(update, {where: where});
};
module.exports = OrderTicketDao;